﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Twi.COMMON.Core
{
    /*
     * CopyRight:©2014 踏影软件开发工作室（Tyingsoft、Twilight、Twifly）版权所有
     * Author:xuzhihong
     * Create Date: 2014-11-05
     * Description: 正则表达式辅助类
     */
    public class TwiRegexHelper
    {
        /// <summary>
        /// 将指定字符串，使用正则表达式替换
        /// </summary>
        /// <param name="checkStr">指定字符串</param>
        /// <param name="pattern">正则表达式</param>
        /// <param name="replaceTo">符合正则表达式的字符，替换为该字符串,默认为""</param>
        /// <param name="onlyFirst">默认所有的都替换，如为true表示仅替换第一个</param>
        /// <returns>替换后的字符串</returns>
        public static string ReplaceRegexToStr(string checkStr, string pattern , string replaceTo = "",bool onlyFirst=false)
        {
            MatchCollection matches = Regex.Matches(checkStr, pattern);
            foreach (Match match in matches)
            {
                checkStr = checkStr.Replace(match.Value, replaceTo);
                if (onlyFirst) {
                    break;
                }
            }
            return checkStr;
        }
    }
}
